package com.bank.um.service.Impl;

import com.bank.entity.Member;
import com.bank.um.mapper.MemberMapper;
import com.bank.um.service.MemberService;
import com.bank.utils.CookieUtils;
import com.bank.utils.SmsUtils;
import com.bank.utils.UUIDUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import static com.bank.utils.ValidatorUtils.*;

/**
 * @author: Lixy
 * @email: 1908598362@qq.com
 * @date: 2021/12/31 星期五 20:25
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Transactional
    public List<Member> listAll() {
        List<Member> members = memberMapper.selectList(null);
        return members;
    }

    @Transactional
    public int addMember(Long age, String name, Long memcardnum, String phone, String password) {
        Member member = new Member();
        member.setName(name);
        member.setAge(age);
        member.setMemcardnum(memcardnum);
        member.setPhone(phone);
        member.setPassword(password);
        int row = memberMapper.insert(member);
        return row;
    }

    public Member SelectByPass(String name, String password) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        queryWrapper.eq("password", password);
        Member member = memberMapper.selectOne(queryWrapper);
        return member;
    }

    public Member SelectByPhone(String phone) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        Member member = memberMapper.selectOne(queryWrapper);
        return member;
    }

    public Member SelectById(String id) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Member member = memberMapper.selectOne(queryWrapper);
        return member;
    }

    public boolean getVcode(String phone, String code) {
        String codeKey = phone + "code";
        String redisCode = String.valueOf(redisTemplate.opsForValue().get(codeKey));
        if (code.equals(redisCode)) {
            return true;
        } else {
            return false;
        }
    }

    public boolean getPcode(String phone,String code){
        String codekey = phone+"message";
        String redisCode = String.valueOf(redisTemplate.opsForValue().get(codekey));
        if (code.equals(redisCode)) {
            return true;
        } else {
            return false;
        }
    }

    public Member SelectByCoolredis(String uuid, HttpServletResponse response, HttpServletRequest request) {
        if (uuid == null) {
            return null;
        }
        Member member = ((Member) redisTemplate.opsForValue().get("member:" + uuid));
        if (member != null) {
            CookieUtils.setCookie(request, response, "MemeberUUID", uuid);
        }
        return member;
    }

    public int UpdateMemPhone(Member member,String newPhone){
        member.setPhone(newPhone);
        int row = memberMapper.updateById(member);
        return row;
    }

    public int UpdateMemNameOrPass(Member member,String newPassword,String newName,String uuid){
        member.setName(newName);
        member.setPassword(newPassword);
        int row = memberMapper.updateById(member);
        redisTemplate.delete("member:" + uuid);
        return row;
    }

    public String addByCookie(HttpServletRequest request,HttpServletResponse response,Member member){
        String uuid = UUIDUtil.uuid();
        if (member != null) {
            //httpServletRequest.getSession().setAttribute(uuid,member);
            redisTemplate.opsForValue().set("member:" + uuid, member, 1800, TimeUnit.SECONDS);
            CookieUtils.setCookie(request, response, "MemeberUUID", uuid);
        }
        return uuid;
    }
}
